<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2021/6/23
 * Time: 10:03
 */

namespace app\admin\library;


class Module
{
    /**
     * 获取Module内容
     * @param $project
     * @param $module
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function module($project, $module)
    {
        $moduleModel = new \app\admin\model\MwModule();
        $columnModel = new \app\admin\model\MwColumn();

        $info = $moduleModel->getModuleInfo($project, $module);
        $module = [];
        $module['api'] = json_decode($info['api'], true) ?? $info['api'];

        $module['name'] = $info['name'];//模块名称
        $module['table_name'] = $info['table_name'];//表名
        $module['alias'] = $info['alias_name'];//表别名
        $module['comment'] = $info['comment'];//表备注
        $module['condition'] = [];//主模块列表页固定不变的静态查询条件   查询表中字段的默认搜索条件
        $columnCondition = $columnModel->getStaticCondition($info['id']);
        if ($columnCondition) {
            $module['condition'] = JsonAnalysis::staticCondition($columnCondition);
        }

        //数据表schema信息
        $columnList = $columnModel->getListModule($info['id']);
        //数据表结构转换映射
        $module['schema'] = DbMap::structureSwitch($columnList);
        if (!empty($info['relation_condition'])){
            $module['relation'] = json_decode($info['relation_condition'], true) ?? $info['relation_condition'];//关联模块
        }else{
            $module['relation'] = [];
        }


        return $module;
    }

}